<?php
	
	function VoteDataCenter_service_main(){
		//Call form :: index.php?module=VoteMember&type=service&func=autent&uname=admin&pass=admin
		$uid       = FormUtil::getPassedValue ('uid', false , 'GET');
		$uname       = FormUtil::getPassedValue ('uname', false , 'GET');		
		$province  = FormUtil::getPassedValue ('province', false , 'GET');		
		$district  = FormUtil::getPassedValue ('district', false , 'GET');
		$tambon    = FormUtil::getPassedValue ('tambon', false , 'GET');				

		//Call first function to access data
		VoteDataCenter_service_province($uid, $uname, $province, $district, $tambon);
		
		//VoteDataCenter_service_createzip($uid, $uname, $province, $district, $tambon);		
		
		pnShutDown();
	}
	

	function VoteDataCenter_service_province($uid, $uname, $province, $district, $tambon){

		$provinceAccessId = array();		
		$provinceAccessId = explode(',', $province);
		$districtAccessId = array();		
		$districtAccessId = explode(',', $district);
		$tambonAccessId = array();		
		$tambonAccessId = explode(',', $tambon);		
			
		
		Loader::loadClass('ProvinceService', "modules/VoteDataCenter/services");		
		$province = new ProvinceService();
		$objectArray = $province->getProvinceById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId);
		$xmlObject = $province->arrayToXML($uid, $uname, $provinceAccessId, $districtAccessId, $tambonAccessId, $objectArray);
		$province->renderXMLResponse($xmlObject);
		
	}

	
	function VoteDataCenter_service_district(){

		$uid       = FormUtil::getPassedValue ('uid', false , 'GET');
		$province  = FormUtil::getPassedValue ('province', false , 'GET');		
		$district  = FormUtil::getPassedValue ('district', false , 'GET');
		$tambon    = FormUtil::getPassedValue ('tambon', false , 'GET');				
		$sp        = FormUtil::getPassedValue ('sp', false , 'GET');			

    //VoteDataCenter_service_getProvinceById($uid,$province);
		//Call VoteMember API  for read permission
		Loader::loadClass('DistrictService', "modules/VoteDataCenter/services");
		
		
		$provinceAccessId = array();		
		$provinceAccessId = explode(',', $province);
		$districtAccessId = array();		
		$districtAccessId = explode(',', $district);
		$tambonAccessId = array();		
		$tambonAccessId = explode(',', $tambon);				
		
		$district    = new DistrictService();
		$objectArray = $district->getDistrictById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $sp);
		$xmlObject   = $district->arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $objectArray);
		$district->renderXMLResponse($xmlObject);		
		
		pnShutDown();
	}

	
	function VoteDataCenter_service_tambon(){

		$uid       = FormUtil::getPassedValue ('uid', false , 'GET');
		$province  = FormUtil::getPassedValue ('province', false , 'GET');		
		$district  = FormUtil::getPassedValue ('district', false , 'GET');
		$tambon    = FormUtil::getPassedValue ('tambon', false , 'GET');	
		$sd        = FormUtil::getPassedValue ('sd', false , 'GET');						

		$provinceAccessId = array();		
		$provinceAccessId = explode(',', $province);
		$districtAccessId = array();		
		$districtAccessId = explode(',', $district);
		$tambonAccessId = array();		
		$tambonAccessId = explode(',', $tambon);		

		Loader::loadClass('TambonService', "modules/VoteDataCenter/services");	
		$tambon = new TambonService();
		$objectArray = $tambon->getTambonById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $sd);
		$xmlObject   = $tambon->arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $objectArray);
		$tambon->renderXMLResponse($xmlObject);			
				
		pnShutDown();		

	}

	function VoteDataCenter_service_village(){

		$uid       = FormUtil::getPassedValue ('uid', false , 'GET');
		$province  = FormUtil::getPassedValue ('province', false , 'GET');		
		$district  = FormUtil::getPassedValue ('district', false , 'GET');
		$tambon    = FormUtil::getPassedValue ('tambon', false , 'GET');	
		$st        = FormUtil::getPassedValue ('st', false , 'GET');					

		$provinceAccessId = array();		
		$provinceAccessId = explode(',', $province);
		$districtAccessId = array();		
		$districtAccessId = explode(',', $district);
		$tambonAccessId = array();		
		$tambonAccessId = explode(',', $tambon);	


		Loader::loadClass('VillageService', "modules/VoteDataCenter/services");
		$village = new VillageService();		
		$objectArray = $village->getVillageById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $st);
		$xmlObject   = $village->arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $objectArray, $st);
		$village->renderXMLResponse($xmlObject);			
				
		pnShutDown();		

	}

/*Not use now
	function VoteDataCenter_service_unit(){

		$uid       = FormUtil::getPassedValue ('uid', false , 'GET');
		$province  = FormUtil::getPassedValue ('province', false , 'GET');		
		$district  = FormUtil::getPassedValue ('district', false , 'GET');
		$tambon    = FormUtil::getPassedValue ('tambon', false , 'GET');	
		$select     = FormUtil::getPassedValue('select', false , 'GET');					
		$select_moo = FormUtil::getPassedValue('select_moo', false , 'GET');						


		//print_r($province);     //VoteDataCenter_service_getProvinceById($uid,$province);
		//Call VoteMember API  for read permission
		Loader::loadClass('UnitService', "modules/VoteDataCenter/services");
				
		//print_r($province); exit;
		$provinceIdArray = array();		
		$provinceIdArray = explode(',', $province);
		
		$districtIdArray = array();		
		$districtIdArray = explode(',', $district);
		
		$tambonIdArray = array();		
		$tambonIdArray = explode(',', $tambon);		
		
		$unit = new UnitService();	
		$unit->getUnitById($select, $uid, $provinceIdArray, $districtIdArray, $tambonIdArray, $select_moo);			


	}	
*/

	
	function VoteDataCenter_service_place(){

		$uid       = FormUtil::getPassedValue ('uid', false , 'GET');
		$province  = FormUtil::getPassedValue ('province', false , 'GET');		
		$district  = FormUtil::getPassedValue ('district', false , 'GET');
		$tambon    = FormUtil::getPassedValue ('tambon', false , 'GET');	
		$st        = FormUtil::getPassedValue ('st', false , 'GET');
		$sm        = FormUtil::getPassedValue ('sm', false , 'GET');	
		$su        = FormUtil::getPassedValue ('su', false , 'GET');						
			
		$provinceAccessId = array();		
		$provinceAccessId = explode(',', $province);
		$districtAccessId = array();		
		$districtAccessId = explode(',', $district);
		$tambonAccessId = array();		
		$tambonAccessId = explode(',', $tambon);				

		Loader::loadClass('PlaceService', "modules/VoteDataCenter/services");
		$place = new PlaceService();
		$objectArray = $place->getPlaceLayer($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $st, $sm, $su);
		$xmlObject   = $place->arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $objectArray, $st, $sm, $su);
		$place->renderXMLResponse($xmlObject);	
		pnShutDown();				

	}

	function VoteDataCenter_service_home(){

		$uid       = FormUtil::getPassedValue ('uid', false , 'GET');
		$province  = FormUtil::getPassedValue ('province', false , 'GET');		
		$district  = FormUtil::getPassedValue ('district', false , 'GET');
		$tambon    = FormUtil::getPassedValue ('tambon', false , 'GET');	
		$st        = FormUtil::getPassedValue ('st', false , 'GET');
		$sm        = FormUtil::getPassedValue ('sm', false , 'GET');	
		$su        = FormUtil::getPassedValue ('su', false , 'GET');									

			
		$provinceAccessId = array();		
		$provinceAccessId = explode(',', $province);
		$districtAccessId = array();		
		$districtAccessId = explode(',', $district);
		$tambonAccessId = array();		
		$tambonAccessId = explode(',', $tambon);				

		Loader::loadClass('HomeService', "modules/VoteDataCenter/services");
		$home = new HomeService();		
		$objectArray = $home->getHomeById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $st, $sm, $su);
		$xmlObject   = $home->arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $objectArray, $st, $sm, $su);
		$home->renderXMLResponse($xmlObject);			
				
		pnShutDown();		
		
	}	


	function VoteDataCenter_service_person(){

		$uid       = FormUtil::getPassedValue ('uid', false , 'GET');
		$province  = FormUtil::getPassedValue ('province', false , 'GET');		
		$district  = FormUtil::getPassedValue ('district', false , 'GET');
		$tambon    = FormUtil::getPassedValue ('tambon', false , 'GET');	
		$st        = FormUtil::getPassedValue ('st', false , 'GET');
		$sm        = FormUtil::getPassedValue ('sm', false , 'GET');	
		$su        = FormUtil::getPassedValue ('su', false , 'GET');
		$sh        = FormUtil::getPassedValue ('sh', false , 'GET');											

			
		$provinceAccessId = array();		
		$provinceAccessId = explode(',', $province);
		$districtAccessId = array();		
		$districtAccessId = explode(',', $district);
		$tambonAccessId = array();		
		$tambonAccessId = explode(',', $tambon);			

		Loader::loadClass('PersonService', "modules/VoteDataCenter/services");
		$person = new PersonService();
		if($st && $sm && $su && $sh){
			//$person->getPersonByMooAndHouseNo($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $st, $sm, $su, $sh);
			$objectArray = $person->getPersonByMooAndUnitAndHouseNo($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $st, $sm, $su, $sh);		
			$xmlObject   = $person->arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $objectArray, $st, $sm, $su, $sh);
			$person->renderXMLResponse($xmlObject);							
		}else if($st && $sm && $su){
			//$person->getPersonByMoo($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $st, $sm, $su);
			$objectArray = $person->getPersonByUnit($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $st, $sm, $su);	
			$xmlObject   = $person->arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $objectArray, $st, $sm, $su, $sh);
			$person->renderXMLResponse($xmlObject);							
		}
    pnShutDown();
	}
		
		
	function VoteDataCenter_service_createzip($uid, $uname, $province, $district, $tambon){	

		//Create zip file for iphone
		//VoteDataCenter_service_createzip($uid, $uname, $province, $district, $tambon);
		 	
		$provinceAccessId = array();		
		$provinceAccessId = explode(',', $province);
		$districtAccessId = array();		
		$districtAccessId = explode(',', $district);
		$tambonAccessId   = array();		
		$tambonAccessId   = explode(',', $tambon);	
		
		//Create folder for store the xml file		
    $xmlRootDirectoryPath = 'xml/';
    mkdir($xmlRootDirectoryPath, 0777);
		
    $xmlDirectoryPath = 'xml/'.$uname."-".$uid."/";
    mkdir($xmlDirectoryPath, 0777);
    
    			
    //Write xml main file	
    $provinceFileName = "province.xml";					
		Loader::loadClass('ProvinceService', "modules/VoteDataCenter/services");		
		$province = new ProvinceService();
		$provinceObjectArray = $province->getProvinceById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId);
		//$provinceXMLObject = $province->arrayToXML($uid, $uname, $provinceAccessId, $districtAccessId, $tambonAccessId, $provinceObjectArray);			
		//$province->saveXMLResponse($xmlDirectoryPath, $provinceFileName, $provinceXMLObject);		
		//unset($province);


		//print_r($provinceObjectArray); echo "<br><br>";
    //Write xml province file	
		Loader::loadClass('DistrictService', "modules/VoteDataCenter/services");		
		$district = new DistrictService();
		foreach($provinceObjectArray as $item){
			$districtFileName    = "pv".$item['id'].".xml";
			$districtObjectArray = $district->getDistrictById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $item['id']);
			$districtTempObjectArray[] = 	$districtObjectArray;
			//$districtXMLObject   = $district->arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $districtObjectArray);						
			//$district->saveXMLResponse($xmlDirectoryPath, $districtFileName, $districtXMLObject);		
		}		
		
		//Retrive district id from inner array\
		//print_r($districtObjectArray); echo "<br><br>";		
		foreach($districtTempObjectArray as $item1){
			foreach($item1 as $item2){
				$districtIdObjectArray[]['id'] = $item2['id'];
			}	
		}			
				
					 
		//print_r($districtIdObjectArray); echo "<br><br>";

    //Write xml district file			
		Loader::loadClass('TambonService', "modules/VoteDataCenter/services");	
		$tambon = new TambonService();		
		foreach($districtIdObjectArray as $item){		
			$tambonFileName     = "dt".$item['id'].".xml";		
			$tambonObjectArray  = $tambon->getTambonById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $item['id']);
			$tambonTempObjectArray[] = 	$tambonObjectArray;			
			//$tambonXMLObject    = $tambon->arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $tambonObjectArray);
			//$tambon->saveXMLResponse($xmlDirectoryPath, $tambonFileName, $tambonXMLObject);		
		}
		
		
		//Retrive tambon id from inner array		
		//print_r($tambonObjectArray); echo "<br><br>";
		foreach($tambonTempObjectArray as $item1){
			foreach($item1 as $item2){
				$tambonIdObjectArray[]['id'] = $item2['id'];
			}	
		}		
		//print_r($tambonIdObjectArray); echo "<br><br>";			
				
		
    //Write xml tambon file			
		Loader::loadClass('VillageService', "modules/VoteDataCenter/services");
		$village = new VillageService();	
		foreach($tambonIdObjectArray as $itemTambon){			
			$villageFileName    = "tb".$itemTambon['id'].".xml";				
			$villageObjectArray = $village->getVillageById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $itemTambon['id']);
			$villageTempObjectArray[] = $villageObjectArray;
			//$villageXMLObject   = $village->arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $villageObjectArray,  $itemTambon['id']);
			//$village->saveXMLResponse($xmlDirectoryPath, $villageFileName, $villageXMLObject);				
		}
		

		//print_r($villageTempObjectArray);  echo  "<br><br><br>";					
		//Retrive village id from inner array	
		$villageDataObjectArray = array();			
		foreach($villageTempObjectArray as $item){			
			$villageDataObjectArray = array_merge($villageDataObjectArray, $item);			
		}		
		
		//print_r($villageDataObjectArray); exit;
		
    //Write xml district file			
			Loader::loadClass('PlaceService', "modules/VoteDataCenter/services");	
			$place = new PlaceService();			
		foreach($villageDataObjectArray as $itemVillage){		
				$tempid = $itemVillage['tambon_id'].$itemVillage['moo'].$itemVillage['unit'];
				$vid = "vl".$tempid;
				$placeFileName     = $vid.".xml";		
				
				$placeObjectArray = $place->getPlaceLayer($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $itemVillage['tambon_id'], $itemVillage['moo'], $itemVillage['unit']);
			$placeTempObjectArray[] = $placeObjectArray;				
				//$placeXMLObject   = $place->arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $placeObjectArray, $itemVillage['tambon_id'], $itemVillage['moo'], $itemVillage['unit']);
				//$place->saveXMLResponse($xmlDirectoryPath, $placeFileName, $placeXMLObject);												
		}		
		
		$placeDataObjectArray = array();		
		foreach($placeTempObjectArray as $item){			
			$placeDataObjectArray = array_merge($placeDataObjectArray, $item);			
		}				
		//print_r($placeDataObjectArray); exit;


	
			Loader::loadClass('HomeService', "modules/VoteDataCenter/services");						
			$home = new HomeService();							
			foreach($placeDataObjectArray as $itemPlace){	
				//Write xml place file							
				//Gen file name
				$tempid = $itemPlace['tambon_id'].$itemPlace['moo'].$itemPlace['unit'];				
				$placeid         = "pl".$tempid.$itemPlace['type'];
				$homeFileName    = $placeid.".xml";																						

				$homeObjectArray = $home->getHomeById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $itemPlace['tambon_id'], $itemPlace['moo'], $itemPlace['unit']);
			$homeTempObjectArray[] = $homeObjectArray;					
				//$homeXMLObject   = $home->arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $homeObjectArray, $itemPlace['tambon_id'], $itemPlace['moo'], $itemPlace['unit']);
				//$home->saveXMLResponse($xmlDirectoryPath, $homeFileName, $homeXMLObject);
			}		
				
				
				
		//$sum = 0;		
		//foreach($homeTempObjectArray as $key=>$item1){	
		//    $sumRow = count($item1);
		//		echo $key."=>".$sumRow."<br>";
		//		$sum += $sumRow;
		//}		
		//echo "#############<br>".$sum;			
		//exit;
		
		
				
		$homeDataObjectArray = array();		
		foreach($homeTempObjectArray as $item1){	
			foreach($item1 as $item2){	
				$homeDataObjectArray[] = $item2;
			}			
		}	
		
		//echo count($homeDataObjectArray); exit;
		
			
		//print_r($homeDataObjectArray);	 exit;
		Loader::loadClass('PersonService', "modules/VoteDataCenter/services");			

		foreach($homeDataObjectArray as $itemHome){
			$person = new PersonService();
				
			//Write xml home file	
			$tempid = $itemHome['tambon_id'].$itemHome['moo'].$itemHome['unit'];													
			$houseNoId = str_replace("/", "-", $itemHome['house_no']);
			$hnid = "hn".$tempid.$houseNoId;
			$personFileName = $hnid.".xml";	
							
			$person->getPersonByMooAndUnitAndHouseNoForWriteXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $itemHome['tambon_id'], $itemHome['moo'], $itemHome['unit'], $itemHome['house_no'], $xmlDirectoryPath, $personFileName);	
			unset($person);	
			//$personXMLObject   = $person->arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $personObjectArray, $itemHome['tambon_id'], $itemHome['moo'], $itemHome['unit'], $itemHome['house_no']);
			//$person->saveXMLResponse($xmlDirectoryPath, $personFileName, $personXMLObject);	
		}								
					

		/////////////////////////////////////
		//Zip all XML file in directory 
		////////////////////////////////////
		//Setting path
		$folderPath = "xml/".$uname."-".$uid;		
		$zipFileName = $uname."-".$uid.".zip";
		$saveFileDirectory = "xml/";
		$filepath = $saveFileDirectory.$zipFileName;
				
		Loader::loadClass('ZipFileService', "modules/VoteDataCenter/services");
		$zip = new ZipFileService();	
		$fileArray=$zip->parseDirectory($folderPath);		
    
		$isCreate = $zip->createZipByFilename($fileArray, $filepath, true);
		//$zip->createZipByFilename($fileArray, "../xml/admin-1.zip", true);

		//$size = $zip->checkFileSize($filepath);
		
	  echo "<br>Launching zip : ";
		if($isCreate){
		  echo "Make file finished.";
		}else{
		  echo "Failed.";		
		}
		
		pnShutDown();

				
	}	
	
?>